var fs = require('fs');
var path = require('path');
var sys_path = path
var cur_path = process.cwd()
//源目录：要移动的文件所在目录
var src_path = process.argv[2];
//目标目录：将文件移动到指定目录
var dest_path = process.argv[3];
//移动文件的规则 "xx|xx" 多个规则中间使用|分割，如果只有一个规则不需要使用|字符
var ruleText = "道具表"
//多个规则的关系：与 1；或 0 
var isAnd = 0
//移动后是否删除源文件 1 删除 0 不删除
var IS_DELETE = 1
console.log(" src_path ", src_path)
console.log(" dest_path ", dest_path)
console.log(" ruleText ", ruleText)
console.log(" isAnd ", isAnd)
var configFileName = 'copy_folder.json'
var configPath = path.join(cur_path,configFileName)
console.log('configPath',configPath)
let ruleList = []
if (ruleText.length > 0 && ruleText.indexOf('|') >= 0) {
    ruleList = ruleText.split('|')
}
//同步写文件
var writeFile = function (fileName, data) {
	fs.writeFileSync(fileName, data, 'utf-8');
}
var readFile = function (fileName) {
	return fs.readFileSync(fileName, 'utf-8');
}
let config = readFile(configPath);
console.log('config',config)
var  fileMap = JSON.parse(config)
var createDir = function (path) {
    //console.log('mkdir path ',path)
    if (path.indexOf(sys_path.sep) >= 0) {
        let list = path.split(sys_path.sep)
        let dir = list[0]
        for (let i = 1; i < list.length; i++) {
            if (list[i].indexOf('.') >= 0 && i == 0) {
                return dir;
            }
            dir = sys_path.join(dir, list[i])
            let stat = fs.existsSync(dir);
            //console.log('dir', dir)
            if (!stat) {
                try {
                    fs.mkdirSync(dir);
                } catch (e) {
                    if (e.code != 'EEXIST') throw e;
                }
            }

        }
    }
    //console.log('error')
    return ''
}

var mkdir = function (p) {
    try {
        fs.mkdirSync(p);
    } catch (e) {
        if (e.code != 'EEXIST') throw e;
    }
}

function copyFile(src, dst) {
    fs.writeFileSync(dst, fs.readFileSync(src));
}

function parseFile(filePath, exportPath, fileName) {
    if (!fileName) {
        return;
    }

	if(fileMap[fileName]){
		return;
	}
	
	fileMap[fileName] = fileName;

    console.log(' parseFile ===================fileName ', fileName)

    copyFile(filePath, path.join(exportPath, fileName))
    if (IS_DELETE) {
        deleteFile(filePath)
    }
}
function deleteFile(fileName) {
    fs.unlinkSync(fileName); //删除文件
}

function readDir(dir, exportPath) {

    // console.log(' readDir ===================')
    var stat = fs.statSync(dir);
    if (!stat.isDirectory()) {
        return;
    }
    var subpaths = fs.readdirSync(dir),
        subpath;
    for (var i = 0; i < subpaths.length; ++i) {
        if (subpaths[i][0] === '.') {
            continue;
        }
        subpath = path.join(dir, subpaths[i]);
        console.log(' readDir   subpath ', subpath,' subpaths[i]', subpaths[i])
        stat = fs.statSync(subpath);
        if (stat.isDirectory()) {
            readDir(subpath, exportPath);
        } else if (stat.isFile()) {
            parseFile(subpath, exportPath, subpaths[i]);
        }
    }
}


function exportAll() {
    createDir(dest_path);
    readDir(src_path, dest_path);
	writeFile(configPath, JSON.stringify(fileMap))
}

if (src_path && dest_path) {
    exportAll();
}